{{template "admin/header.html"}}
<title>update - GPRESS</title>
{{template "admin/bodystart.html"}}
{{ $tableFiled := tableFiled .urlPathParam }}
{{$fieldType := fieldType}}
<section class="section">
	<div class="container">
		<div class="table-content">
			<div class="column">
				<form id="gpress-form" action="{{basePath}}admin/{{.urlPathParam}}/update" method="POST">
					{{ $data := . }}
					{{ range $tableFiled }}

					{{if eq .FieldCode "createTime"}}
					  {{continue}} 
					{{end}}
					
					{{if eq .FieldCode "updateTime"}}
					  {{continue}} 
					{{end}}

					<div class="field is-horizontal flex-center">
						<div class="field-left" style="width: 180px;">
							<label class="label">{{.FieldName}}</label>
							<input type="hidden" id="fieldType_{{.FieldCode}}" value="{{.FieldType}}" />
						</div>
						<div class="field-right" style="width: 340px;">
							<div class="field">
								<div class="control">
									{{if eq .FieldCode "fieldType"}}
									<select name="{{.FieldCode}}" class="input">
										{{ range $key,$value := $fieldType }}
										<option value={{$key}}>{{$value}}</option>
										{{end}}
									</select>

										

										{{else if eq .FieldCode "id"}}
											<input class="input" readonly="readonly" type="text" name="{{.FieldCode}}"
												id="{{.FieldCode}}" value="{{ index $data .FieldCode }}">
										{{else}}
												<input class="input" type="text" name="{{.FieldCode}}"
													id="{{.FieldCode}}" value="{{ index $data .FieldCode }}">
										{{end}}
								</div>
							</div>
						</div>
					</div>

					{{end}}

					<div class="field is-grouped" style="padding-left: 190px;">
						<div class="control">
							<button type="submit" class="button is-primary">提交</button>
						</div>
						<div class="control">
							<button type="reset" class="button is-info is-light">重置</button>
						</div>
					</div>

				</form>
			</div>
		</div>
	</div>
</section>

<script>
	const form = document.getElementById('gpress-form')

	form.addEventListener('submit', async event => {
		event.preventDefault()
		const formData = new FormData(form)

		const jsonObject = {}
		for (const [key, value] of formData.entries()) {
			const fieldType = document.querySelector('#fieldType_' + key).value
			if (!!fieldType && fieldType == 1) {
				jsonObject[key] = parseInt(value)
			}
			//if (key == "sortNo" ||key == "status"||key == "fieldType"||key == "required"){
			//  jsonObject[key] = parseInt(value);
			//}
			else {
				jsonObject[key] = value
			}
			//jsonObject[key] = value;
		}
		const response = await fetch(form.action, {
			method: form.method,
			body: JSON.stringify(jsonObject),
			headers: { 'Content-Type': 'application/json', }
		})

		let res = await response.json()
		if (res.statusCode == 1) {
			$.confirm({
				useBootstrap:false,
				boxWidth: '400px',
				title: '提示',
				content: '修改成功！',
				type: 'green',
				buttons: {
					'确定': function() {
						window.location.href = '{{basePath}}admin/{{.urlPathParam}}/list'
					}
				}
			})
		}
	})
</script>





{{template "admin/bodyend.html"}}
